package com.yifeng.repo.controller.kafka.configure;

import com.yifeng.repo.controller.kafka.message.KafkaProduceManager;
import com.yifeng.repo.controller.kafka.message.KafkaPullConsumeManager;
import com.yifeng.repo.controller.kafka.web.KafkaServletRegistrar;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by daibing on 2024/4/29.
 */
@Configuration
@EnableConfigurationProperties(KafkaProperties.class)
@ConditionalOnClass({KafkaProperties.class})
public class KafkaConfiguration {

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private KafkaProperties properties;

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = KafkaProperties.PREFIX, name = "enable-produce-manager")
    public KafkaProduceManager kafkaProduceManager() {
        return new KafkaProduceManager(applicationName, properties);
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = KafkaProperties.PREFIX, name = "enable-pull-consume-manager")
    public KafkaPullConsumeManager kafkaPullConsumeManager() {
        return new KafkaPullConsumeManager(applicationName, properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = KafkaProperties.PREFIX, name = "enable-mq-api")
    public KafkaServletRegistrar kafkaServletRegistrar() {
        return new KafkaServletRegistrar(properties.getMqAdminPrefix());
    }
}
